// Copyright (C) 2010 Félix Cloutier.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License 2.0 for more details.

// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/

#ifndef UCONTEXT_COROUTINE
#define UCONTEXT_COROUTINE

#include "Coroutine.h"

// <ucontext.h> was deprecated by some pedant idiot of the POSIX committee, and
//	left people who used it with no real alternative. So we're going to use it
//	anyways. If it ever ceases working, someone will just have to reimplement
//	that using POSIX threads. Which is just so much less convenient (and
//	possibly much slower). Hardware threads aren't designed for cooperative
//	threading.

// This is an OS X workaround: including <ucontext.h> triggers an error
// When we'll support other POSIX systems, we'll need to #ifdef this
#include <sys/ucontext.h>

#define COROUTINE_CLASS		UContextCoroutine

namespace LLVMJit
{

class UContextCoroutine : public Coroutine
{
	unsigned char* stack;
	ucontext_t context;
	UContextCoroutine* theOther;
	
	UContextCoroutine(UContextCoroutine* that);
	
public:
	UContextCoroutine(size_t stackSize = 0x8000);
	
	virtual void Yield();
	virtual void SetEntryPoint(EntryPoint entryPoint, void* argument);
	
	~UContextCoroutine();
};

}

#endif